package com.songcha.library_common.util

import android.app.*
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Build
import androidx.core.app.NotificationCompat

class NotificationUtil {
    companion object{
        /**
         * 开启前台服务并发送通知
         */
        @JvmStatic
        fun createForegroundNotification(context:Service,icon:Int,title:String,msg:String,
                                         channelId:String, channelName:String,
                                         channelDesc:String,intent:Intent,showBadge:Boolean,
                                         onGoing:Boolean,autoCancel:Boolean,progress:Int,
                                         channelPriority:Int,notificationPriority:Int):Notification{
            //8.0及以上注册通知渠道
            createNotificationChannel(context, channelId, channelName,channelDesc,showBadge,channelPriority)
            val notification = createForegroundNotification(context,icon,title,msg,
                channelId, intent,onGoing,autoCancel,progress,notificationPriority)
            return notification
        }

        //将服务置于启动状态 ,NOTIFICATION_ID指的是创建的通知的ID
        @JvmStatic
        fun startForeground(context:Service,notificationId:Int,notification: Notification){
            context.startForeground(notificationId, notification)
        }


        fun getNotificationManager(context:Service):NotificationManager{
            val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            return notificationManager
        }


        /**
         * 创建通知渠道
         */
        @JvmStatic
        private fun createNotificationChannel(context:Context,channelId:String, name:String,desc:String,
                                              showBadge:Boolean,priority:Int){
            val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            //Android8.0以上的系统，新建消息通道
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
                //用户可见的通道名称
                val channelName = name
                //通道的重要程度
                //low不播放声音
                val importance: Int = priority//NotificationManager.IMPORTANCE_LOW
                //构建通知渠道
                val notificationChannel = NotificationChannel(channelId,
                    channelName, importance)
                notificationChannel.description = desc
                //角标
                notificationChannel.setShowBadge(showBadge)
                //LED灯
                notificationChannel.enableLights(true)
                notificationChannel.lightColor = Color.RED
                //震动,第一个参数为等待时间，第二个参数为时长
                notificationChannel.vibrationPattern = longArrayOf(0)
                notificationChannel.enableVibration(false)
                //notificationChannel.setSound(null,null)
                //向系统注册通知渠道，注册后不能改变重要性以及其他通知行为
                notificationManager.createNotificationChannel(notificationChannel)
            }
        }

        /**
         * 创建服务通知
         */
        @JvmStatic
        private fun createForegroundNotification(context:Context,icon:Int,title:String,msg:String,
                                                 channelId:String,intent:Intent,onGoing:Boolean,
                                                 autoCancel:Boolean,progress:Int,priority: Int): Notification {
            //intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK

            val pendingIntent: PendingIntent = PendingIntent.getActivity(context,
                1,intent, PendingIntent.FLAG_UPDATE_CURRENT)

            val builder: NotificationCompat.Builder = NotificationCompat.Builder(context, channelId)
                //通知小图标
                .setSmallIcon(icon)
                //通知标题
                .setContentTitle(title)
                //通知内容
                .setContentText(msg)
                //设置通知显示的时间
                .setWhen(System.currentTimeMillis())
                //设定启动的内容
                .setContentIntent(pendingIntent)
                //设置通知优先级
            builder.priority = priority//NotificationCompat.PRIORITY_DEFAULT
            //设置为进行中的通知，true不能删除
            builder.setOngoing(onGoing)
                //点击通知栏自动消失
                .setAutoCancel(autoCancel)
                .setVibrate(longArrayOf(0))


           if(progress>0)
               builder.setProgress(100,progress,false)
            //创建通知并返回

            return builder.build()
        }
    }
}